using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using BusinessClient;
using Common;

namespace AdminizzaUIDesktop.form
{
    public partial class ClientManager : AdminizzaUIDesktop.UIForm
    {
        private BizClient biz;
        Client clients;
        private DataTable personas; 
        private DataTable telefonos;
        private DataTable direcciones;
        
        public ClientManager():base()
        {
            InitializeComponent();
            base.InitializeComponent();
        }

        private void ClientManager_Load(object sender, EventArgs e)
        {
            try
            {
                biz = new BizClient(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Adminizza\AdminizzaSandBox\Common\AdminizzaDB.mdf;Integrated Security=True;User Instance=True");
                clients = biz.GetClients();
                this.LoadData();
                this.ShowDataGrids();
                this.FilterPerson(1);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
            }
        }
        //TO-DO
        private void FilterPerson(int personId)
        {
           
        }

        private void ShowDataGrids()
        {
            this.dgvPersons.DataSource = personas;
            this.dgvAddress.DataSource = direcciones;
            this.dgvPhones.DataSource = telefonos;

            //Oculto columnas innecesarias de Direcciones
            this.dgvPhones.Columns[0].Visible = false;
            this.dgvPhones.Columns[1].Width = this.dgvPhones.Width - 3;
            this.dgvPhones.Columns[2].Visible = false;
            this.dgvPhones.ColumnHeadersVisible = false;
            this.dgvPhones.RowHeadersVisible = false;
            //Oculto columnas inncesearias de Telefonos
            this.dgvAddress.Columns[0].Visible = false;
            this.dgvAddress.Columns[1].Visible = false;
            this.dgvAddress.Columns[2].Width = this.dgvAddress.Width - 3;
            this.dgvAddress.ColumnHeadersVisible = false;
            this.dgvAddress.RowHeadersVisible = false;
            //Oculto columnas inncesearias de Personas
            this.dgvPersons.Columns[0].Visible = false;
            this.dgvPersons.Columns[1].Visible = false;
            this.dgvPersons.Columns[2].Width = (this.dgvPersons.Width - 3) / 2;
            this.dgvPersons.Columns[3].Width = (this.dgvPersons.Width - 3) / 2;
            this.dgvPersons.ColumnHeadersVisible = false;
            this.dgvPersons.RowHeadersVisible = false;
        }

        private void LoadData()
        {   
            personas = clients.Personas;
            telefonos = clients.Telefonos;
            direcciones = clients.Direcciones;
        }

        private void btnAddPhone_Click(object sender, EventArgs e)
        {
            this.ShowActionButtons();
            this.dgvPhones.Rows.Add();
            this.dgvPhones.ReadOnly = false;
        }
        private void btnAddAddress_Click(object sender, EventArgs e)
        {
            this.ShowActionButtons();
            this.ClearDataGrids();
        }
        private void btnAddPerson_Click(object sender, EventArgs e)
        {
            this.ShowActionButtons();
            this.ClearDataGrids();
        }
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            this.HideActionButtons();
            biz.InsertClient(clients);
            this.ShowDataGrids();

        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.HideActionButtons();
            this.ShowDataGrids();
        }
        
        //TODO:Debo limpiar las grillas.
        private void ClearDataGrids()
        {
        }

        private void ShowActionButtons()
        {
            this.btnConfirm.Visible = true;
            this.btnCancel.Visible = true;
        }

        private void HideActionButtons()
        {
            this.btnConfirm.Visible = false;
            this.btnCancel.Visible = false;
        }

        private void btnAddClient_Click(object sender, EventArgs e)
        {
            this.ShowActionButtons();
            this.ClearDataGrids();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            clients = biz.GetClients(atbFilter.TextValue);
            LoadData();
        }
    }
}

